package com.example.demo;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/help")
@RestController
public class UserController {
    @RequestMapping( value = "/hello", method = RequestMethod.GET)
    public String hello(){
        return "hello";
    }

    @RequestMapping("r1")
    public String r1(String name) {
        return "接收到参数 name ：" + name;
    }


    @RequestMapping("r2")
    public String r2(Integer age) {
        return "接收到参数 age ：" + age;
    }


    @RequestMapping("r3")
    public String r3(Integer age , String name) {
        return "接收到参数 age ：" + age + "  " + "接收参数name :" + name;
    }

    @RequestMapping("r4")
    public String r4(User user) {
        return user.toString();
    }

    @RequestMapping("r5")
    public String r5(Integer age , @RequestParam(value = "name" ,required = false) String username) {
        return "接收到参数 age ：" + age + "  " + "接收参数username :" + username;
    }


    @RequestMapping("r6")
    public String r6(String[] arr){
        return Arrays.toString(arr);
    }


    @RequestMapping("r7")
    public String r7(@RequestParam(value = "list" , required = false) List<String> list){

        if(list != null){
            return list.toString();
        }
        return "list为空";
    }

    @RequestMapping("/r8")
    public String r8(@RequestBody User user) {
        return user.toString();
    }

    @RequestMapping("/r9/{articleId}")
    public String r9(@PathVariable Integer articleId) {
        return "articleId :" + articleId;
    }

    @RequestMapping("/r11")
    public String r11(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("D:/Temp/"+ fileName));
        return "获取上传文件： " + file.getOriginalFilename();

    }
}

